<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport"
        content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0" />
    <title>在线问诊</title>
    <link rel="stylesheet" type="text/css" href="../../css/api.css" />
    <link rel="stylesheet" type="text/css" href="../../css/public.css" />
    <script type="text/javascript" src="../../script/chat/lib-flexible.js"></script>
    <style>
        html,
        body {
            background: #fff;
        }

        .chat-header-title {
            background: #fff;
            height: auto;
            width: 100%;
            position: fixed;
            z-index: 10000;
        }

        .chat-header-title .title {
            position: relative;
            width: 100%;
            height: 1rem;
        }

        .chat-header-title .title .backward {
            position: absolute;
            left: 0.4rem;
            top: 0;
            width: 0.45rem;
            height: 0.5rem;
            padding: 0.2rem 0.25rem;
            background: url(../../image/ucenter/goback.png);
            background-position: center center;
            background-size: .5rem .5rem;
            background-repeat: no-repeat;
            z-index: 10;
        }

        .chat-header-title .title .subtitle {
            position: absolute;
            text-align: center;
            top: 0;
            width: 96%;
            display: inline-block;
            font-size: 0.4rem;
            padding: 0.2rem;
            color: #000000;
        }

        .chat-header-title .avatar {
            position: relative;
            width: 100%;
            padding: 0.2rem 0;
            text-align: center;
        }

        .chat-header-title .avatar img {
            width: 1.5rem;
            height: 1.5rem;
            border-radius: 50%;
        }

        .chat-header-title .status {
            position: relative;
            width: 100%;
            color: #000;
            font-weight: bold;
            font-size: 0.45rem;
            text-align: center;
        }

        .chat-header-title .desc {
            position: relative;
            font-size: 0.35rem;
            padding: 0.2rem 1.2rem;
            color: #9F9E9E;
            text-align: center;
        }

        .chat-header-title .chatting {
            position: relative;
            display: flex;
            margin: 0;
            padding: 0.2rem;
        }

        .chat-header-title .chatting .consult {
            height: auto;
            font-size: 0.36rem;
            padding: 0 0.4rem;
            width: 7rem;
            background: url(../../image/chat/dot.png);
            background-position: left center;
            background-size: .3rem .3rem;
            background-repeat: no-repeat;
            font-weight: normal;
        }

        .chat-header-title .chatting .consult i {
            color: #9F9E9E;
            font-style: normal;
            margin-left: 0.1rem;
        }

        .chat-header-title .chatting .praise {
            height: auto;
            font-size: 0.36rem;
            color: #294AC2;
            width: 1.6rem;
            padding: 0 0 0 0.6rem;
            background: url(../../image/chat/praise.png);
            background-position: left center;
            background-size: .45rem .45rem;
            background-repeat: no-repeat;
            font-weight: normal;
        }

        /*语音发送状态*/
        .chat .record {
            box-sizing: border-box;
            position: fixed;
            z-index: 9999;
            top: 30%;
            left: 0px;
            right: 0px;
            margin: 0 auto;
            border-radius: 10px;
            padding: 16px;
            width: 50%;
            text-align: center;
            overflow: hidden;
            background-color: rgba(0, 0, 0, 0.5);
            display: none;
        }

        .chat .record img {
            height: 100px;
        }

        .chat .record .upper .title {
            color: #000;
            font-size: 15px;
            line-height: 160%;
            padding: 5px;
        }

        .chat .record .release {
            display: none;
        }

        .chat .record .release .title {
            color: #000;
            font-size: 16px;
            line-height: 150%;
            padding: 5px;
            background-color: rgb(132, 52, 20, .5);
        }

        .chat {
            padding: 2rem .42rem;
            background-color: #fff !important;
        }

        .chat .chat-item {
            position: relative;
            width: 100%;
            margin-bottom: .42rem;
            overflow: hidden;
            display: block;
        }

        .chat .chat-item:last-child {
            margin-bottom: 1.6rem;
        }

        .chat .chat-header {
            text-align: center;
            margin-bottom: .42rem;
        }

        .chat .chat-header>span {
            font-size: .35rem;
            font-weight: 400;
            color: rgba(204, 204, 204, 1);
            line-height: .54rem;
            background-color: #F7F7F7;
            padding: .10rem .20rem;
            border-radius: .06rem;
        }

        .chat .chat-left {
            float: left;
        }

        .chat .chat-right {
            float: right;
        }

        .chat .chat-media {
            display: inline-block;
            max-width: 2rem;
        }

        .chat .chat-media img {
            width: 100%;
            border-radius: 50%;
        }

        .chat .chat-inner {
            position: relative;
        }

        .chat .chat-arrow {
            content: '';
            position: absolute;
            top: .35rem;
        }

        .chat .chat-left .chat-arrow {
            width: 0;
            height: 0;
            border-width: .15rem .31rem .15rem 0;
            border-style: solid;
            border-color: transparent #f2f0f0 transparent transparent;
            left: -0.28rem;
        }

        .chat .chat-right .chat-arrow {
            width: 0;
            height: 0;
            border-width: .15rem 0 .15rem .31rem;
            border-style: solid;
            border-color: transparent transparent transparent #1ca0f2;
            right: -0.28rem;
        }

        .chat .chat-left .chat-content {
            font-size: .35rem;
            font-weight: 400;
            color: rgba(25, 25, 25, 1);
            border-radius: .14rem;
            min-height: 1rem;
            position: relative;
            max-width: 90%;
            word-break: break-all;
            word-wrap: break-word;
            margin-top: .3rem;
        }

        .chat .chat-left .chat-content .body {
            line-height: .59rem;
            padding: .3rem .29rem .3rem .29rem;
            border-radius: .14rem;
        }

        .chat .chat-left .chat-contentimg {
            background: rgba(243, 243, 243, 0);
            /*float: left;*/
            left: 0.5rem;
            margin-left: .2rem;
        }

        .chat .chat-left .chat-contentimg img {
            height: 3.08rem;
            border-radius: 0.14rem;
        }

        .chat .chat-right .chat-contentimg {
            background: rgba(243, 243, 243, 0);
            /*float: right;*/
            right: 0.5rem;
            margin-right: .2rem;
        }

        .chat .chat-right .chat-contentimg img {
            border-radius: 0.14rem;
            height: 3.09rem;
        }



        .chat .chat-left .chat-content-video {
            background: rgba(243, 243, 243, 0);
            /*float: left;*/
            left: 0.5rem;
            margin-left: .2rem;
        }

        .chat .chat-left .chat-content-video .play-icon {
            position: absolute;
            border-radius: 0.14rem;
            height: 3.09rem;
            width: 1.8rem;
            left: 0.2rem;
            top: 0;
            background: url("../../image/defaults/play.png");
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }

        .chat .chat-left .chat-content-video video {
            height: 3.08rem;
            width: 1.8rem;
            border-radius: 0.14rem;
        }

        .chat .chat-right .chat-content-video {
            background: rgba(243, 243, 243, 0);
            /*float: right;*/
            right: 0.5rem;
            margin-right: .2rem;
        }

        .chat .chat-right .chat-content-video .play-icon {
            position: absolute;
            border-radius: 0.14rem;
            height: 3.09rem;
            width: 1.8rem;
            right: 0.2rem;
            top: 0;
            background: url("../../image/defaults/play.png");
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }

        .chat .chat-right .chat-content-video video {
            border-radius: 0.14rem;
            height: 3.09rem;
            width: 1.8rem;
        }


        .body-by {
            font-size: .3rem;
        }

        .chat .chat-left .icon {
            position: relative;
            min-width: 1.8rem;
            height: 1.11rem;
        }

        .chat .chat-left .icon .iconhron {
            position: absolute;
            width: 1.11rem;
            height: 1.11rem;
            left: 0px;
            bottom: 0px;
            background: url(../../image/icon/icon_chat_lefthorn.png);
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }

        .chat .chat-left .icon .iconhrons {
            position: absolute;
            width: 1.11rem;
            height: 1.11rem;
            left: 0px;
            bottom: 0px;
            background: url(../../image/icon/icon_chat_lefthorn.gif);
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }

        .chat .chat-right .chat-content {
            font-size: .35rem;
            font-weight: 400;
            color: rgba(255, 255, 255, 1);
            border-radius: .14rem;
            min-height: 1rem;
            position: relative;
            max-width: 90%;
            word-break: break-all;
            word-wrap: break-word;
            margin-top: .3rem;
        }

        .chat .chat-right .chat-content .body {
            line-height: .59rem;
            padding: .3rem .29rem .3rem .29rem;
            border-radius: .14rem;
        }

        .chat .chat-right .icon {
            position: relative;
            min-width: 1.8rem;
            height: 1.11rem;
        }

        .chat .chat-right .icon .iconhron {
            position: absolute;
            width: 1.11rem;
            height: 1.11rem;
            right: 0px;
            bottom: 0px;
            background: url(../../image/icon/icon_chata_righthorn.png);
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }

        .chat .chat-right .icon .iconhrons {
            position: absolute;
            width: 1.11rem;
            height: 1.11rem;
            right: 0px;
            bottom: 0px;
            background: url(../../image/icon/icon_chata_righthorn.gif);
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }


        .chat .chat-content .body img {
            max-width: 80%;
            display: block;
        }

        .chat .chat-content .body video {
            width: 100%;
            display: block;
            object-fit: scale-down;
        }

        .chat .chat-status {
            position: relative;
            width: 1.11rem;
            height: 1.11rem;
            line-height: 1.11rem;
            text-align: center;
        }

        .chat .chat-status .iconfont {
            position: absolute;
            width: 1.11rem;
            height: 1.11rem;
            left: 0px;
            bottom: 0px;
            background: url(../../image/icon/icon_fail.png);
            background-position: center center;
            background-size: .61rem .61rem;
            background-repeat: no-repeat;
        }

        .chat .chat-left .chat-status {
            top: .3rem;
            left: 0.5rem;
            float: left;
        }

        .chat .chat-status .badge {
            width: .13rem;
            height: .13rem;
            border-radius: 50%;
            background-color: #ff2600;
            text-decoration: none;
            display: table-cell;
            text-align: center;
            vertical-align: middle;
        }

        .chat .chat-status .times {
            font-size: .32rem;
            font-weight: 400;
            color: rgba(153, 150, 150, 1);
            line-height: .5rem;
        }

        .chat .chat-left .chat-media {
            width: 1.11rem;
            float: left;
        }

        .chat .chat-left .chat-inner {
            max-width: 80%;
        }

        .chat .chat-left .chat-content {
            background: #f2f0f0;
            float: left;
            left: 0.5rem;
        }

        .chat .chat-left .chat-contentimg {
            background: rgba(255, 255, 255, 0);
            float: left;
            left: 0.5rem;
            margin-left: .2rem;
        }

        .chat .chat-left .chat-status .badge {
            position: absolute;
            z-index: 99;
            top: 0;
            left: .29rem;
        }

        .chat .chat-left .chat-status .times {
            position: absolute;
            z-index: 99;
            bottom: 0;
            left: .29rem;
        }

        .chat .chat-right .chat-media {
            width: 1.11rem;
            float: right;
        }

        .chat .chat-right .chat-inner {
            float: right;
            max-width: 80%;
        }

        .chat .chat-right .chat-content {
            background: #1ca0f2;
            right: 0.5rem;
            float: right;
        }

        .chat .chat-right .chat-contentimg {
            background: rgba(243, 243, 243, 0);
            float: right;
            right: 0.5rem;
            margin-right: .2rem;
        }

        .chat .chat-right .chat-status {
            float: right;
            top: .3rem;
            right: 0.5rem;
        }

        .chat .chat-right .chat-status .badge {
            position: absolute;
            z-index: 99;
            top: 0;
            right: .29rem;
        }

        .chat .chat-right .chat-status .times {
            position: absolute;
            z-index: 99;
            bottom: 0;
            right: .29rem;
        }

        .list-enter-active,
        .list-leave-active {
            transition: all 1s;
        }

        .list-enter,
        .list-leave-to {
            opacity: 0;
        }

        .btnDiv {
            position: fixed;
            bottom: 0;
            left: 0;
            width: 100%;
            /* background-color: skyblue; */
            margin-top: 10rem;
        }

        .btnDiv span {
            font-size: 16px;
            padding: 2px 10px;
            background-color: #f2f0f0;
            display: inline-block;
            margin-left: 0.2rem;
            border-radius: 200px;
            margin-bottom: 14px;
        }

        .tab {
            width: 100%;
            display: flex;
            flex-direction: row;
            justify-content: space-around;
            padding: 0.4rem 0;
        }

        .tab span {
            font-size: 14px;
            padding: 2px 10px;
            background-color: #e8e7e7;
            color: #333;
            border-radius: 50px;
            /* width: 33.3%; */
            display: inline-block;
            text-align: center;
        }

        .chat .chat-content .card {
            width: 100%;
            border-radius: .14rem;
            color: #333;
            background-color: #F7F7F7;
        }

        .chat .chat-content .card .card_title {
            height: 1rem;
            line-height: 1rem;
            background-color: #e3ebee;
            padding: 0 0.2rem;
            border-radius: .14rem .14rem 0 0;
        }

        .chat .chat-content .card .card_title img {
            width: 0.5rem;
            height: 0.5rem;
            float: left;
            margin-top: 0.25rem;
        }

        .chat .chat-content .card .card_title .title_name {
            font-size: 0.5rem;
            font-weight: bold;
            margin: 0 0.3rem;
        }

        .chat .chat-content .card .card_title .service_name {
            font-size: 0.4rem;
        }

        .chat .chat-content .card .card_main {
            margin: 0 0.3rem;
            padding: 0.3rem 0;
            border-bottom: 1px dashed #ccc;
        }

        .chat .chat-content .card .tips {
            width: 100%;
            height: 1rem;
            font-size: 0.3rem;
            line-height: 1rem;
            color: #666;
            padding: 0 0.3rem;
        }

        .chat .chat-content .card .tips img {
            width: 0.5rem;
            height: 0.5rem;
            float: left;
            border-radius: 50%;
            margin-right: 0.3rem;
            margin-top: 0.25rem;
        }

        .time {
            float: right;
            font-size: 10px;
            position: absolute;
            top: 0;
            color: #ccc;
        }

        .right {
            right: 1.2rem;
        }

        .left {
            left: 1.2rem;
        }

        .dangan {
            position: fixed;
            width: 100%;
            height: 100%;
            background-color: #fff;
            top: 0;
            left: 0;
            color: #333;
            font-size: 20px;
            text-align: center;
        }

        .dangan .patient {
            margin-top: 3rem;
        }

        .active {
            background-color: #294AC2 !important;
            color: #fff !important;
        }

        .historyList {
            position: fixed;
            width: 100%;
            height: 100%;
            background-color: #fff;
            top: 0;
            left: 0;
            color: #333 !important;
            text-align: center;
            font-size: 14px;
        }

        .historyList div {
            margin-top: 3rem;
        }

        .chufang_card {
            height: 1rem !important;
        }

        .chufang_card .subname {
            font-size: 0.5rem;
            color: #666;
        }

        .chufang_card .status {
            float: left;
            width: 1rem;
            height: 1rem;
            /* background-color: #e9f0f8; */
            position: relative;
            text-align: center;
        }

        .chufang_card .status img {
            width: 0.5rem !important;
            height: 0.5rem !important;
            margin-left: 0.25rem;
            display: inline-block;
        }

        .chufang_card .status .text {
            position: absolute;
            width: 100%;
            height: 0.5rem;
            text-align: center;
            line-height: 0.5rem;
            background-color: #ff2600;
            bottom: 0;
            left: 0;
        }
    </style>
</head>

<body>
    <div id="chat">
        <div class="chat-header-title">
            <div class="title">
                <div class="backward" tapmode onclick="api.closeWin()"></div>
                <div class="subtitle">
                    <span v-if="patient">{{patient.name}} {{patient.sex}} {{patient.age}}</span>
                    <span v-else>问诊咨询</span>
                </div>
            </div>
            <!-- tab切换 -->
            <div class="tab">
                <span @click="current = 0" @click="pageDown(200)" :class="current == 0 ? 'active' : ''">问诊对话</span>
                <!-- <span @click="current = 1" :class="current == 1 ? 'active' : ''">诊疗记录</span> -->
                <span @click="current = 2" :class="current == 2 ? 'active' : ''">患者档案</span>
            </div>
            <!--问诊中状态start-->
            <div class="chatting" v-if="status == 2">
                <span class="consult">问诊中<i>24小时后结束</i></span>
                <span class="praise">患者详情</span>
            </div>
            <!--问诊中状态end-->
        </div>
        <section class="chat" v-cloak>
            <div class="record" id="record" v-if="record==1">
                <div class="upper" id="upper" v-if="release==0">
                    <img src="../../image/icon/iconupper.gif" alt="" />
                    <div class="title">上滑取消发送</div>
                </div>
                <div class="release" id="release" v-if="release==1">
                    <img src="../../image/icon/iconrelease.png" alt="" />
                    <div class="title">松开手指取消发送</div>
                </div>
            </div>
            <div class="chat-item" v-for="(chats,index) in chat" v-if="current == 0"
                :class="chats.direction=='send'?'chat-right':'chat-left'">
                <div class="chat-header" v-if="chats.time!=null"><span>{{chats.time|formatTime}}</span></div>
                <!-- <div class="chat-media">
                    <img :src="chats.direction == 'send' ? IMAGE_HTTP + doctor.avatar : IMAGE_HTTP + user.avatar" />
                </div> -->
                <div class="chat-media" v-if="chats.direction=='send'">
                    <img :src="doctor.avatar" />
                </div>
                <div class="chat-inner flex-wrap">
                    <!-- 消息发送状态 -->
                    <div class="chat-status" v-if="chats.direction=='send'">
                        <i class="iconfont icon-correct"></i>
                    </div>
                    <div class="chat-status" v-if="chats.direction=='send'&&chats.body.type=='voice'">
                        <!-- 语音是否已读 -->
                        <!-- <div class="badge" v-if="chats.isReadAcked==false"></div> -->
                        <div class="times">{{chats.body.duration}}'</div>
                    </div>
                    <!-- 图片消息 放在发送语音zip解压出来的chat_frm.html文件-->
                    <div class="chat-contentimg" v-if="chats.body.type=='image'"
                        @click="fnOpenImg(chats.body.remotePath)">
                        <img :src="chats.body.remotePath" />
                    </div>
                    <!-- 文本消息 -->
                    <div class="chat-content"
                        v-if="(chats.body.type=='text') && (chats.ext.card != 1 && chats.ext.card != 2)">
                        <div class="chat-arrow"></div>
                        <div class="body">{{chats.body.text}}</div>
                    </div>
                    <!-- 问诊卡消息 -->
                    <div class="chat-content" v-if="chats.body.type=='text' && chats.ext.card == 1"
                        @click="current = 2">
                        <div class="card">
                            <div class="card_title">
                                <img src="../../image/icon_class.png" alt="">
                                <span class="title_name">问诊卡</span>
                                <span class="service_name" v-if="chats.body.text.servicesName=='image'">图文问诊</span>
                                <span class="service_name" v-if="chats.body.text.servicesName=='audio'">语音问诊</span>
                                <span class="service_name" v-if="chats.body.text.servicesName=='video'">视频问诊</span>
                            </div>
                            <div class="card_main">
                                <p class="patient">患者：{{chats.body.text.patient}}
                                    <span v-if="chats.body.text.sex==1">男</span>
                                    <span v-if="chats.body.text.sex==2">女</span>
                                    {{chats.body.text.age}}岁
                                </p>
                                <p class="miaoshu">病情描述：{{chats.body.text.diseaseInfo}}</p>
                            </div>
                            <p class="tips">
                                <img src="../../image/logo.png" alt="">
                                祥愿堂保证您的隐私，请放心问诊
                            </p>
                        </div>
                    </div>
                    <!-- 处方卡片消息 -->
                    <div class="chat-content" v-if="chats.body.type=='text' && chats.ext.card == 2">
                        <div class="card">
                            <div class="card_title chufang_card">
                                <div class="status">
                                    <img src="../../image/icon_class.png" alt="">
                                    <!-- <div class="text">已购买</div> -->
                                </div>
                                <span class="title_name" style="font-size: 16px!important;"
                                    v-if="chats.body.text.title == 1">用药方案-内服中药</span>
                                <span class="title_name" style="font-size: 16px!important;"
                                    v-if="chats.body.text.title == 2">用药方案-外用中药</span>
                                <!-- <span class="subname"
                                    style="font-size: 12px!important;text-align: right;margin-left: 0.4rem;">{{chats.body.text.time}}</span> -->
                            </div>
                            <div class="card_main">
                                <p class="patient">患者：{{chats.body.text.userName}}
                                    <span style="margin: 0 0.2rem;">男</span>
                                    {{chats.body.text.userAge}}岁
                                </p>
                                <p class="miaoshu">
                                    诊断: {{chats.body.text.diseases}}；{{chats.body.text.dialectical}}</p>
                                <p class="miaoshu">
                                    开方时间: {{chats.body.text.time}}</p>
                            </div>
                            <p class="tips">
                                <img src="../../image/logo.png" alt="">
                                祥愿堂保证您的隐私，请放心问诊
                            </p>
                        </div>
                    </div>
                    <!-- 视频消息 -->
                    <div class="chat-content-video" v-if="chats.body.type=='video'"
                        @click="fnPlayVideo(chats.body.localPath,chats.body.remotePath,chats.conversationId,chats.messageId);">
                        <div class="play-icon"></div>
                        <video :src="chats.body.remotePath" id="play-video" style="background-color: #333;" />
                    </div>
                    <!-- 语音消息 -->
                    <div class="chat-content" v-if="chats.body.type=='voice'"
                        @click="fnPlayVoice(index,chats.body.localPath,chats.body.remotePath,chats.conversationId,chats.messageId);">
                        <div class="chat-arrow"></div>
                        <div class="icon">
                            <div class="iconhron" :class="{iconhrons:chats.ext.isShow==true}"></div>
                        </div>
                    </div>
                    <div class="chat-status" v-if="chats.direction!='send'&&chats.body.type=='voice'">
                        <!-- 语音是否已读 -->
                        <div class="badge" v-if="chats.isReadAcked==false"></div>
                        <div class="times">{{chats.body.duration}}``</div>
                    </div>
                    <!-- 消息发送状态 -->
                    <div class="chat-status" v-if="chats.direction!='send'">
                        <i class="iconfont icon-correct"></i>
                    </div>
                </div>
            </div>
            <div class="historyList" v-if="current == 1">
                <div>暂无记录</div>
            </div>
            <div class="dangan" v-if="current == 2">
                <p class="patient">患者：{{formCard.name}}
                    <span v-if="formCard.sex == 1">男</span>
                    <span v-else>女</span>
                    {{formCard.age || 0}}岁
                </p>
                <p>身高：{{formCard.height || 0}}cm 体重：{{formCard.weight || 0}}kg</p>
                <p>血型：{{formCard.bloodGroup || '未知'}} 婚姻状况：{{formCard.wedlock || '未知'}}</p>
                <p>过往病史：{{formCard.medicalHistory || '未知'}}</p>
                <p>过敏史：{{formCard.allergyHistory || '未知'}}</p>
                <p>家族病史：{{formCard.familyHistory || '未知'}}</p>
                <p>个人习惯：{{formCard.personalHabits || '未知'}}</p>
            </div>

        </section>
        <div class="btnDiv">
            <span @click="kaifang()">开具处方</span>
            <!-- <span>快捷回复</span> -->
        </div>
        <!-- <div style="width: 100%;background-color: slateblue;">
                <br>
            </div> -->
    </div>
</body>
<script type="text/javascript" src="../../script/api.js"></script>
<script type="text/javascript" src="../../script/vue.js"></script>
<script type="text/javascript" src="../../script/hb.js"></script>
<!-- 消除移动端点击延迟300ms -->
<script type="text/javascript" src="../../script/chat/fastclick.js"></script>
<!-- 公共js -->
<script type="text/javascript" src="../../script/chat/common.js"></script>
<!-- 环信接口 -->
<script type="text/javascript" src="../../script/chat/easeChat.js"></script>
<script type="text/javascript">
    var UIChatBox, frameHeight, eRecord, eUpper, eRelease, inputBarHeightsum = 50,
        isSwice = false,
        isShow = false;
    // 定义一个全局的聊天数组
    var message_data = [];
    apiready = function () {
        fnInitVue();
    };

    function fnInitVue() {
        window.ListVue = new Vue({
            el: '#chat',
            data: {
                doctor: {
                    name: "",
                    department: "",
                    avatar: "../../image/kefu.png",
                    doctorUserId: $api.getStorage("emp").userId
                },
                consults: {},
                patient: null,
                user: {
                    name: "",
                    avatar: "../../image/defaults/default_pic.png",
                    userId: 0
                },
                status: 0, //待支付，待接诊，问诊中
                chat: [],
                record: 0, //点击录音状态
                release: 0, //取消显示状态
                current: 0,
                formCard: "", // 处方卡片
                IMAGE_HTTP: IMAGE_HTTP,
            },
            created: function () {
                var $this = this;
                if(!api.pageParam.userId){
                   toast("请与患者建立会话联系");
                   return;
                }
                if(!api.pageParam.consultationId){
                   toast("咨询订单走丢了");
                   return;
                }
                if ($api.getStorage("emp").userId) {
                    get_ajax("portal/doctor/view", {
                        id: $api.getStorage("emp").userId
                    }, function (res, err) {
                        if (res.code == 2000 && res.data) {
                            $this.doctor = res.data
                            $this.doctor.avatar = res.data.avatar ? IMAGE_HTTP + res.data.avatar : "../../image/kefu.png"
                            $this.doctor.name = res.data.name ? res.data.name : "";
                            $this.doctor.department = res.data.department ? res.data.department : ""
                        }
                    })
                }
                // 获得问诊单信息
                if (api.pageParam.consultationId) {
                    get_ajax("portal/consult/detail", {
                        id: api.pageParam.consultationId
                    }, function (res, err) {
                        if (res.data && res.code == 2000) {
                            $this.consults = res.data;
                            $this.addFriend(res.data.userId);
                            //获得用户信息
                            get_ajax("user/view", {
                                userId: res.data.userId
                            }, function (rts, err) {
                                //alert(JSON.stringify(rts));
                                if (rts.code == 200) {
                                    $this.user.avatar = rts.data.avatar ? IMAGE_HTTP + rts.data.avatar : "";
                                    $this.user.name = rts.data.nickname ? rts.data.nickname : "";
                                }
                            });
                            // 根据档案id查询档案详情
                            get_ajax("ucenter/patient/view", {
                                id: res.data.patientId
                            }, function (ret, err) {
                                if (ret.data && ret.code == 2000) {
                                    $this.formCard = ret.data
                                }
                            })
                        }
                    })
                }
                api.addEventListener({
                    name: 'card'
                }, function (ret, err) {
                    var str = {
                        "title": ret.value.title,
                        "time": ret.value.time,
                        "userName": ret.value.userName,
                        "userAge": ret.value.userAge,
                        "userSex": ret.value.userSex,
                        "diseases": ret.value.diseases,
                        "dialectical": ret.value.dialectical,
                        "patientId": ret.value.patientId
                    }
                    // var str = JSON.stringify(ret.value)
                    $this.fnSendText(str, 2)
                });
                $this.status = typeof $this.consults.status != "undefined" ? $this.consults.status : 0;
                //获取环信聊天消息
                $this.fnMessageFromDB(api.pageParam.conversationsId);
                //初始化下拉加载
                $this.fnsetRefreshHeaderInfo();
                $this.fnAddMessageListener();
            },
            mounted: function () {
                var that = this;
                this.$nextTick(function () {
                    //初始化数据
                    that.fnInit();
                    //设置页面到最顶部
                    that.resizeFrame(inputBarHeightsum);
                    //获取环信聊天消息
                    var conversationsId = api.pageParam.conversationsId ? api.pageParam.conversationsId : api.pageParam.userId;
                    this.fnMessageFromDB(conversationsId);
                    //初始化下拉加载
                    that.fnsetRefreshHeaderInfo();
                });
            },
            methods: {
                fnInit: function () {
                    var that = this;
                    UIChatBox = api.require('UIChatBox'); //引用UIChatBox模块
                    frameHeight = api.frameHeight; //获取frame高度
                    eRecord = $api.byId('record'); //语音弹出dom id
                    eUpper = $api.byId('upper');
                    eRelease = $api.byId('release');
                    this.fnUIChatBox();
                    var easeChat = api.require('easeChat');
                    easeChat.addMessageListener({
                        name: 'receive'
                    }, function (ret) {
                        if (ret.messages) {
                            // 向聊天消息追加一条消息
                            that.fnAddanew(ret.messages, true);
                        }
                    });
                },

                //发送文本消息
                fnEaseChatSendText: function (conversationId_, text_, from_, to_, ext) {
                    var that = this;
                    var easeChat = api.require('easeChat');
                    easeChat.getConversation({
                        conversationId: conversationId_,
                        type: "chat",
                        ifCreate: true
                    }, function (ret, err) {
                        if (ret.status) {
                            var conversationId = ret.conversation.conversationId
                            easeChat.sendText({
                                conversationId: conversationId, //此字段为会话id一开始可以对话可以为空
                                chatType: "chat", //会话类型chat：单聊会话 groupChat：群聊会话 chatRoom：聊天室会话
                                text: text_, //发送的消息内容
                                from: from_, //发送方当前登录账号
                                to: to_, //接受方
                                ext: ext ? ext : {}// //JSON 对象；消息扩展，Key值类型必须是NSString, Value值类型必须是NSString或者 NSNumber类型的 BOOL, int, unsigned in, long long, double
                            }, function (ret, err) {
                                if (ret) {
                                    if (ret.status == true) {
                                        // 向聊天消息追加一条消息
                                        that.fnAddanew([ret.message], true);

                                        //将指定消息设置为已读
                                        fnMarkMessageAsRead(ret.message.conversationId, ret.message
                                            .chatType, ret.message.messageId);
                                    } else {
                                        toast("消息发送失败请稍后再试");
                                    }
                                } else {
                                    toast("糟糕，开小差了:" + err.msg);
                                }
                            });
                        } else {
                            toast("糟糕，开小差了");
                        }
                    });
                },
                //监听消息事件
                fnAddMessageListener: function () {
                    var that = this;
                    var easeChat = api.require('easeChat');
                    easeChat.addMessageListener({
                        name: 'receive'
                    }, function (ret) {
                        if (ret) {
                            that.fnAddanew(ret.messages, true);
                        } else {
                            toast("糟糕，开小差了:" + ret.msg);
                        }
                    });
                },

                //发送语音消息
                fnEaseChatSendVoice: function (conversationId_, path_, from_, to_, time_,ext) {
                    var that = this;
                    var date = new Date();
                    var y = date.getFullYear();
                    var m = date.getMonth();
                    var d = date.getDate();
                    var h = date.getHours();
                    var mm = date.getMinutes();
                    var s = date.getSeconds();
                    // 根据发送人名和发送时间，合成的语音文件名
                    var displayName = from_ + "" + y + "" + m + "" + d + "" + h + "" + mm + "" + s;
                    var easeChat = api.require('easeChat');
                    easeChat.getConversation({
                        conversationId: conversationId_,
                        type: "chat",
                        ifCreate: true
                    }, function (ret, err) {
                        if (ret.status) {
                            var conversationId = ret.conversation.conversationId
                            easeChat.sendVoice({
                                conversationId: conversationId,
                                chatType: "chat",
                                length: time_,
                                path: path_,
                                displayName: displayName,
                                from: from_,
                                to: to_,
                                ext: ext ? ext : {}
                            }, function (ret, err) {
                                if (ret) {
                                    if (ret.status == true) {
                                        // 向聊天消息追加一条消息

                                        that.fnAddanew([ret.message], true);
                                        //将指定消息设置为已读
                                        fnMarkMessageAsRead(ret.message.conversationId, ret.message.chatType, ret.message.messageId);
                                    } else {
                                        toast("消息发送失败请稍后再试");
                                    }
                                } else {
                                    toast("糟糕，开小差了：" + err.msg);
                                }
                            });
                        } else {
                            toast("糟糕，开小差了：" + err.msg);
                        }
                    });
                },

                //发送视频消息
                fnEaseChatSendVideo: function (conversationId_, path_, from_, to_,ext) {
                    var that = this;
                    var date = new Date();
                    var y = date.getFullYear();
                    var m = date.getMonth();
                    var d = date.getDate();
                    var h = date.getHours();
                    var mm = date.getMinutes();
                    var s = date.getSeconds();
                    // 根据发送人名和发送时间，合成的语音文件名
                    var displayName = from_ + "" + y + "" + m + "" + d + "" + h + "" + mm + "" + s;
                    var easeChat = api.require('easeChat');
                    easeChat.getConversation({
                        conversationId: conversationId_,
                        type: "chat",
                        ifCreate: true
                    }, function (ret, err) {
                        if (ret.status) {
                            var conversationId = ret.conversation.conversationId
                            easeChat.sendVideo({
                                conversationId: conversationId,
                                chatType: "chat",
                                ext: ext ? ext : "",
                                path: path_,
                                displayName: displayName,
                                from: from_,
                                to: to_
                            }, function (ret, err) {
                                if (ret) {
                                    if (ret.status == true) {
                                        // 刷新一下首页列表消息

                                        that.fnAddanew([ret.message], true);
                                        //将指定消息设置为已读
                                        fnMarkMessageAsRead(ret.message.conversationId, ret.message.chatType, ret.message.messageId);
                                        api.hideProgress();
                                    } else {
                                        toast("即时通讯：消息发送失败请稍后再试");
                                        api.hideProgress();
                                    }
                                } else {
                                    toast("即时通讯：" + err.msg);
                                    api.hideProgress();
                                }
                            });
                        } else {
                            toast("糟糕，开小差了：" + err.msg);
                        }
                    });
                },

                // 发送图片消息
                fnEaseChatSendImage: function (conversationId_, path_, from_, to_,ext) {
                    var that = this;
                    var easeChat = api.require('easeChat');
                    easeChat.getConversation({
                        conversationId: conversationId_,
                        type: "chat",
                        ifCreate: true
                    }, function (ret, err) {
                        if (ret.status) {
                            var conversationId = ret.conversation.conversationId
                            easeChat.sendImage({
                                conversationId: conversationId, //此字段为会话id一开始可以对话可以为空
                                chatType: "chat", //会话类型chat：单聊会话 groupChat：群聊会话 chatRoom：聊天室会话
                                path: path_, //发送的消息内容
                                from: from_, //发送方当前登录账号
                                to: to_, //接受方
                                ext: ext ? ext : {} // //JSON 对象；消息扩展，Key值类型必须是NSString, Value值类型必须是NSString或者 NSNumber类型的 BOOL, int, unsigned in, long long, double
                            }, function (ret, err) {
                                if (ret) {
                                    if (ret.status == true) {

                                        that.fnAddanew([ret.message], true);
                                        // //将指定消息设置为已读
                                        fnMarkMessageAsRead(ret.message.conversationId, ret.message.chatType, ret.message.messageId);
                                    } else {
                                        toast("消息发送失败请稍后再试");
                                    }
                                } else {
                                    toast("糟糕，开小差了：" + err.msg);
                                }
                            });
                        } else {
                            toast("糟糕，开小差了：" + err.msg);
                        }
                    });

                },
                //调用UIChatBox模块
                fnUIChatBox: function () {
                    var that = this;
                    UIChatBox.open({
                        placeholder: '',
                        autoFocus: false,
                        maxRows: 4,
                        emotionPath: 'widget://res/img/emotion',
                        texts: {
                            recordBtn: {
                                normalTitle: '按住说话',
                                activeTitle: '松开发送'
                            },
                            sendBtn: {
                                title: '发送'
                            }
                        },
                        styles: {
                            topDivider: {
                                width: 0,
                                color: '#F2F2F2'
                            },
                            inputBar: {
                                borderColor: '#F2F2F2',
                                bgColor: '#F2F2F2'
                            },
                            inputBox: {
                                borderColor: '#fff',
                                bgColor: '#fff',
                                boardBgColor: '#fff',
                                topMargin: 8,
                            },
                            emotionBtn: {
                                normalImg: 'widget://image/icon/icon_chat_expression.png'
                            },
                            extrasBtn: {
                                normalImg: 'widget://image/icon/icon_chat_plus.png'
                            },
                            keyboardBtn: {
                                normalImg: 'widget://image/icon/icon_chata_keyboard.png'
                            },
                            speechBtn: {
                                normalImg: 'widget://image/icon/icon_chat_voice.png',
                                activeImg: 'widget://image/icon/icon_chata_keyboard.png'
                            },
                            recordBtn: {
                                normalBg: '#eee',
                                activeBg: '#aaa',
                                color: '#666',
                                size: 16
                            },
                            indicator: {
                                target: 'both',
                                color: '#c4c4c4',
                                activeColor: '#9e9e9e'
                            },
                            sendBtn: {
                                titleColor: '#fff',
                                bg: '#00acff',
                                activeBg: '#00acff',
                                titleSize: 13
                            }
                        },
                        extras: {
                            titleSize: 12,
                            titleColor: '#999',
                            btns: [{
                                    title: '图片',
                                    normalImg: 'widget://image/icon/icon_chat_img.png',
                                    activeImg: 'widget://image/icon/icon_chat_img.png'
                                }, {
                                    title: '拍照',
                                    normalImg: 'widget://image/icon/icon_chat_picture.png',
                                    activeImg: 'widget://image/icon/icon_chat_picture.png'
                                },
                                /* {
                                                    title: '位置',
                                                    normalImg: 'widget://image/icon/icon_chat_bmap.png',
                                                    activeImg: 'widget://image/icon/icon_chat_bmap.png'
                                                }, */
                                {
                                    title: '视频',
                                    normalImg: 'widget://image/icon/icon_chat_video.png',
                                    activeImg: 'widget://image/icon/icon_chat_video.png'
                                }
                            ]
                        }
                    }, function (ret, err) {
                        if (ret) {
                            if (ret.eventType === "send") {
                                // 发送文本
                                that.fnSendText(ret.msg, 0);
                            }
                            if (ret.eventType === "clickExtras" && ret.index === 0) {
                                // 发送图片
                                that.fnSendImage();
                            }
                            if (ret.eventType === "clickExtras" && ret.index === 1) {
                                // 拍照
                                that.fnCamera();
                            }
                            // if (ret.eventType === "clickExtras" && ret.index === 2) {
                            //     // 发送位置
                            //     fnSendBmap()
                            // }
                            if (ret.eventType === "clickExtras" && ret.index === 2) {
                                // 发送视频
                                that.fnSendVideo()
                            }
                        } else {
                            toast('糟糕，开小差了：！' + JSON.stringify(ret));
                        }
                    });

                    //监听输入框弹动事件
                    UIChatBox.addEventListener({
                        target: 'inputBar',
                        name: 'move'
                    }, function (ret, err) {
                        if (ret) {
                            inputBarHeightsum = ret.inputBarHeight + ret.panelHeight;
                            that.resizeFrame(inputBarHeightsum);
                        }
                    });

                    //监听点击录音按钮功能、按下录音按钮
                    UIChatBox.addEventListener({
                        target: 'recordBtn',
                        name: 'press'
                    }, function (ret, err) {
                        if (ret) {
                            // 显示浮动的录音状态
                            that.record = 1;
                            // 打开震动
                            api.notification({
                                vibrate: [10, 50]
                            });
                            // 判断麦克风是否禁用
                            var resultList = api.hasPermission({
                                list: ['microphone']
                            });
                            if (resultList[0]['granted']) {
                                that.fnOpenAudio();
                                isShow = true; //暂停可以发送
                            } else {
                                api.alert({
                                    title: '麦克风没有准备好',
                                    msg: '请检查是否允许录音',
                                }, function () {
                                    that.fnOpenAudio();
                                    isShow = true; //暂停可以发送
                                    isJuris('microphone', '麦克风');
                                });
                            }
                        } else {
                            toast("糟糕，开小差了：" + JSON.stringify(err));
                        }
                    });

                    //监听点击录音按钮功能、松开录音按钮发送语音消息
                    UIChatBox.addEventListener({
                        target: 'recordBtn',
                        name: 'press_cancel'
                    }, function (ret, err) {
                        if (ret) {
                            that.record = 0;
                            if (isShow) {
                                that.fnStopsendAudio(); //暂停录音并且发送
                                isShow = false; //回到初始状态
                            }
                        }
                    });

                    //监听点击录音按钮功能、按下录音按钮后，从按钮移出
                    UIChatBox.addEventListener({
                        target: 'recordBtn',
                        name: 'move_out'
                    }, function (ret, err) {
                        if (ret) {
                            that.record = 1;
                            that.release = 1;
                            isShow = false;
                        } else {
                            toast("糟糕,开小差了:" + JSON.stringify(err));
                        }
                    });

                    //监听点击录音按钮功能、按下录音按钮后，从按钮移出并松开按钮
                    UIChatBox.addEventListener({
                        target: 'recordBtn',
                        name: 'move_out_cancel'
                    }, function (ret, err) {
                        if (ret) {
                            that.record = 0;
                            that.release = 0;
                            isShow = false; //暂停取消发送
                        } else {
                            toast("糟糕，开小差了：" + JSON.stringify(err));
                        }
                    });

                    //监听点击录音按钮功能、move_out 事件后，重新移入按钮区域
                    UIChatBox.addEventListener({
                        target: 'recordBtn',
                        name: 'move_in'
                    }, function (ret, err) {
                        if (ret) {
                            that.record = 1;
                            that.release = 0;
                            isShow = true; //暂停取消发送
                        }
                    });
                }, //enduibox
                fnsetRefreshHeaderInfo: function () {
                    var that = this;
                    api.setRefreshHeaderInfo({
                        loadingImg: 'widget://image/refresh.png',
                        bgColor: '#fff',
                        textColor: '#ccc',
                        textDown: '下拉刷新...',
                        textUp: '松开刷新...'
                    }, function (ret, err) {
                        //在这里从服务器加载数据，加载完成后调用api.refreshHeaderLoadDone()方法恢复组件到默认状态
                        api.refreshHeaderLoadDone();
                        if (ret) {
                            //获取第一条（最早的一条）
                            if (message_data.length > 0) {
                                var _lastid = message_data[0].messageId;
                                // 调取环信聊天消息接口
                                var conversationId = api.pageParam.conversationId ? api.pageParam.conversationId : api.pageParam.userId;
                                that.fnMessageFromDB(conversationId, true, _lastid);
                            }
                        } else {
                            toast("糟糕，出错啦:" + err.msg);
                        }
                    });
                },
                // 加载数据
                fnLoadMessageFromDB: function (_ret, _loadMore) {
                    if (_ret.messages.length == 0) {
                        toast("没有更多了");
                        return;
                    }
                    // 对数据进行渲染
                    this.fnUpdateMessageFromDB(_ret.messages, _loadMore);
                },
                //获取聊天消息
                fnMessageFromDB: function (_conversationid, _loadMore, _lastid) {
                    var that = this;
                    var easeChat = api.require('easeChat');
                    easeChat.loadMessageFromDB({
                        conversationId: _conversationid,
                        type: "chat",
                        startMsgId: _lastid,
                        pagesize: 6 //没加载6条信息
                    }, function (ret, err) {
                        if (ret) {
                            // 加载chat_frm页面fnLoadMessageFromDB聊天消息
                            that.fnLoadMessageFromDB(ret, _loadMore);
                        } else {
                            toast("即时通讯：" + err.msg);
                        }
                    });
                },

                //渲染数据
                fnUpdateMessageFromDB: function (data_, _loadMore) {
                    var that = this;
                    var eMessageFromDB = data_;
                    //新旧数组拼接
                    message_data = data_.concat(message_data);
                    // 定义俩条消息超过俩分钟间隔显示发送时间
                    for (var i = 0; i < eMessageFromDB.length; i++) {
                        if (i == 0) {
                            eMessageFromDB[i].time = eMessageFromDB[i].localTime;
                        } else {
                            var timevalue = eMessageFromDB[i].localTime - eMessageFromDB[i - 1].localTime;
                            if (timevalue > 120000) {
                                eMessageFromDB[i].time = eMessageFromDB[i].localTime;
                            }
                        }
                    }
                    if (_loadMore) {
                        // 有追加数据，在原有的数据前面连接追加数据
                        that.chat = eMessageFromDB.concat(that.chat);
                        for (var i = 0; i < that.chat.length; i++) {
                            if (that.chat[i].ext.card == "1" || that.chat[i].ext.card == "2") {
                                that.chat[i].body.text = JSON.parse(decodeURIComponent(that.chat[i].body.text))
                            }
                        }
                    } else {
                        //没有追加直接显示
                        that.chat = eMessageFromDB;
                        for (var i = 0; i < that.chat.length; i++) {
                            if (that.chat[i].ext.card == "1" || that.chat[i].ext.card == "2") {
                                that.chat[i].body.text = JSON.parse(decodeURIComponent(that.chat[i].body.text))
                            }
                        }
                    }
                },

                //追加一条最新数据
                fnAddanew: function (data_, addanew_) {
                    var that = this;
                    var eMessageFromDB = [];
                    if (typeof data_[0] != undefined && Array.isArray(data_[0])) {
                        eMessageFromDB = data_[0]
                    } else {
                        eMessageFromDB = data_
                    }
                    if (eMessageFromDB[0].ext.card == "1" || eMessageFromDB[0].ext.card == "2") {
                        eMessageFromDB[0].body.text = JSON.parse(decodeURIComponent(eMessageFromDB[0].body.text))
                    }
                    // alert(JSON.stringify(eMessageFromDB[0]))
                    //新旧数组拼接
                    message_data = data_.concat(message_data);

                    // 定义俩条消息超过俩分钟间隔显示发送时间
                    if (message_data.length != 0) {
                        var timevalue = parseFloat(eMessageFromDB[0].localTime) - parseFloat(message_data.pop().localTime);
                        if (timevalue > 120000) {
                            eMessageFromDB[0].time = eMessageFromDB[0].localTime;
                        }
                    } else {
                        eMessageFromDB[0].time = eMessageFromDB[0].localTime;
                    }
                    if (addanew_) {
                        // 追加最新一条
                        that.chat = that.chat.concat(eMessageFromDB);
                        //设置页面到最顶部
                        that.resizeFrame(inputBarHeightsum);
                    }
                },

                addFriend: function (userId) {
                    get_ajax("huanxin/im/doctor/friend/add", {
                        ownUser: $api.getStorage("emp").userId,
                        friendUser: userId,
                    }, function (ret, errs) {
                        if (ret.code == 2000) {
                            //建立好友关系
                            var easeChat = api.require('easeChat');
                            easeChat.setAutoAcceptFriendInvitation({
                                isAutoAcceptFriendInvitation: true
                            });
                            easeChat.addContact({
                                name: "祥愿堂互联网医院",
                                message: '欢迎问诊'
                            }, function (ret, err) {
                                if (ret.status) {
                                    toast('您与患者建立了医患关系');
                                }
                            });
                        }
                    });
                },
                translateText: function (text) {
                    if (text == '' || text == undefined) {
                        return;
                    }
                    msg = text;
                    var emotionObj = emotion;
                    var regx = /\[(.*?)\]/gm;
                    var translateMSg = text.replace(regx, function (math) {
                        var imgSrc = emotionObj[math];
                        if (!imgSrc) {
                            return math;
                        }
                        var img = '<img src="../../res/img/emotion/' + imgSrc +
                            '.png" class="emotion">'
                        return img;
                    })
                    return translateMSg;
                },
                fnOpenImg: function (img) {
                    // alert(JSON.stringify(img))
                    // var prompt = {
                    //     winName: "chat_win",
                    //     frameName: "chat_frm",
                    //     title: "图片浏览",
                    //     msg: img,
                    //     clickName: "fnOpenClose",
                    //     right: "",
                    // }
                    // var name = "popup";
                    // popup(prompt, name);
                    var UIPhotoViewer = api.require('UIPhotoViewer');
                    UIPhotoViewer.open({
                        images: [
                            img
                        ],
                        placeholderImg: 'widget://res/img/apicloud.png',
                        bgColor: '#000'
                    }, function (ret, err) {
                        if (ret) {
                            if (ret.eventType == 'click') {
                                UIPhotoViewer.close();
                            }
                        }
                    });
                },

                fnPlayVoice: function (index, localPath, remotePath, conversationid, messageid) {
                    // 判断是否是第一次点击播放录音去掉红色提示圆圈，
                    // 设置为已读必须满足是接收消息，
                    // 已经点击的无法再点击改变
                    // （这个功能目前来说没有字段可以实现暂时放着）
                    // if (this.chat[index].isReadAcked == false && this.chat[index].direction == "receive") {
                    //     this.chat[index].isReadAcked = true;
                    //     // 将指定消息设置为已读
                    //     // fnMarkMessageAsRead(conversationid, messageid)
                    //     return;
                    // }
                    //  此块安卓会出现问题，发送语音消息的时候，录音网络地址没有
                    //  所有你发送一次录音正确，
                    //  在发送都会出现是前一次的录音
                    //  这块录音建议发送到自己服务器上然后在播放录音
                    var that = this;
                    if (remotePath != '') {
                        api.download({
                            url: remotePath,
                            savePath: 'fs://easeChat_chat.amr',
                            report: false,
                            cache: true,
                            allowResume: false
                        }, function (ret, err) {
                            if (ret.state == 1) {
                                // 只打开点击的录音，其他的都关闭
                                for (var i = 0; i < that.chat.length; i++) {
                                    if (i == index) {
                                        that.chat[index].ext.isShow = true;
                                        // // 播放语音
                                        api.startPlay({
                                            path: ret.savePath
                                        }, function (ret, err) {
                                            if (ret) {
                                                that.chat[index].ext.isShow = false;
                                                //播放完以后自动停止
                                                api.stopPlay();
                                            } else {
                                                toast("糟糕，开小差了:" + JSON.stringify(err));
                                            }
                                        });
                                    } else {
                                        that.chat[i].ext.isShow = false;
                                    }
                                }
                            }
                        });
                        return;
                    } else {
                        // 只打开点击的录音，其他的都关闭
                        for (var i = 0; i < that.chat.length; i++) {
                            if (i == index) {
                                that.chat[index].ext.isShow = true;
                                // // 播放语音
                                api.startPlay({
                                    path: localPath
                                }, function (ret, err) {
                                    if (ret) {
                                        that.chat[index].ext.isShow = false;
                                        //播放完以后自动停止
                                        api.stopPlay();
                                    }
                                });
                            } else {
                                that.chat[i].ext.isShow = false;
                            }
                        }
                        return;
                    }
                    // console.log(index + "****localPath:" + localPath + "****remotePath:" + remotePath + "****" + conversationid + "****" + type + "****" + messageid)
                },
                fnPlayVideo: function (localPath, remotePath, conversationid, messageid) {
                    var that = this;
                    if (remotePath != '') {
                        api.showProgress({
                            style: 'default',
                            animationType: 'fade',
                            title: '视频下载中...',
                            text: '请稍等...',
                            modal: true
                        });
                        api.download({
                            url: remotePath,
                            savePath: 'fs://' + messageid + '.mp4',
                            report: false,
                            cache: true,
                            allowResume: false
                        }, function (ret, err) {
                            if (ret.state == 1) {
                                // 只打开点击的视频，其他的都关闭ret.savePath ret.fileSize,ret.percent,ret.progress
                                api.hideProgress();
                                playVideo(ret.savePath);
                            } else {
                                tost("糟糕，开小差了:" + JSON.stringify(ret));
                            }
                        });
                        return;
                    } else {
                        // 只打开点击的视频，其他的都关闭ret.savePath ret.fileSize
                        playVideo(localPath);
                        return;
                    }
                }, //endfnPlayVideo

                // 发送位置
                fnSendBmap: function () {},
                // 发送视频消息
                fnSendVideo: function () {
                    var that = this;
                    var UIAlbumBrowser = api.require('UIAlbumBrowser');
                    UIAlbumBrowser.open({
                        max: 1,
                        type: "video",
                        styles: {
                            bg: '#fff',
                            mark: {
                                icon: '',
                                position: 'bottom_left',
                                size: 20
                            },
                            nav: {
                                bg: 'rgba(0,0,0,0.6)',
                                titleColor: '#fff',
                                titleSize: 18,
                                cancelColor: '#fff',
                                cancelSize: 16,
                                finishColor: '#fff',
                                finishSize: 16
                            }
                        },
                        rotation: true
                    }, function (ret) {
                        if (ret.eventType == "confirm") {
                            api.showProgress({
                                style: 'default',
                                animationType: 'fade',
                                title: '发送中...',
                                text: '请稍等...',
                                modal: true
                            });
                            // 调取环信发送文本接口
                            var ext = {
                                "fromId": String($api.getStorage("emp").userId),
                                "toId": String(api.pageParam.userId),
                                "consultationId": String(api.pageParam.consultationId),
                                "chatName": that.doctor.name,
                                "department": that.doctor.department,
                                "avatar": that.doctor.avatar,
                                "hospital": that.doctor.hospital,
                                "jobtitle": that.doctor.jobtitle
                            }
                            var currentUserId = $api.getStorage("emp").userId;
                            var userId = api.pageParam.userId;
                            var conversationsId = (typeof api.pageParam.conversationsId == "undefined") ? String(userId) : api.pageParam.conversationsId;
                            that.fnEaseChatSendVideo(conversationsId, ret.list[0].path,currentUserId,userId,ext);
                        }
                    });
                },
                //  拍照发送
                fnCamera: function () {
                    var that = this;
                    var trs = isJuris('camera', '需要获取您的相机权限，才能上传照片，请问是否同意？'); //相机权限
                    if (!trs) {
                        toast("获取相机权限失败")
                        return;
                    }
                    var resultList = api.hasPermission({
                        list: ['camera'],
                    });
                    api.requestPermission({
                        list: ['camera'],
                        code: 1
                    }, function (ret, err) {
                        if (ret) {
                            api.getPicture({
                                sourceType: 'camera',
                                encodingType: 'jpg',
                                mediaValue: 'pic',
                                targetWidth: '780',
                                targetHeight: '1040',
                                destinationType: 'url',
                                quality: 70,
                                saveToPhotoAlbum: false
                            }, function (ret, err) {
                                if (ret && ret.data) {
                                  // 调取环信发送文本接口
                                  var ext = {
                                      "fromId": String($api.getStorage("emp").userId),
                                      "toId": String(api.pageParam.userId),
                                      "consultationId": String(api.pageParam.consultationId),
                                      "chatName": that.doctor.name,
                                      "department": that.doctor.department,
                                      "avatar": that.doctor.avatar,
                                      "hospital": that.doctor.hospital,
                                      "jobtitle": that.doctor.jobtitle
                                  }

                                  var currentUserId = $api.getStorage("emp").userId;
                                  var userId = api.pageParam.userId;
                                  var conversationsId = (typeof api.pageParam.conversationsId == "undefined") ? String(userId) : api.pageParam.conversationsId;
                                    that.fnEaseChatSendImage(conversationsId, ret.data,currentUserId, userId, ext);
                                }
                            });
                        }
                    })
                }, //endfncamera

                // 去开方
                kaifang: function () {
                    api.openWin({
                        name: "kaifang_win",
                        url: "widget://html/fangan/kaifang_win.html",
                        pageParam: {
                            userId: api.pageParam.userId,
                            consultationId: api.pageParam.consultationId
                        }
                    })
                },
                // 设置最新消息数
                fnNewsBadge: function (_number) {
                    if (_number >= 0) {
                        $api.setStorage('unread_total', _number);
                    }
                    if (_number < 99) {
                        $api.setStorage('show_unread_total', _number);
                    } else {
                        $api.setStorage('show_unread_total', '99+');
                    }
                },
                resizeFrame: function (height) {
                    api.setFrameAttr({
                        name: api.frameName,
                        rect: {
                            marginLeft: 0,
                            marginRight: 0,
                            marginTop: api.pageParam.headerH,
                            h: frameHeight - height
                        }
                    });
                    this.pageDown(200);
                },
                //滑动到底部
                pageDown: function (time) {
                    setTimeout(function () {
                        api.pageDown({
                            bottom: true,
                            animate: true
                        })
                    }, time || 0)
                },
                // 打开录音语音
                fnOpenAudio: function () {
                    api.startRecord({
                        path: 'fs://easeChat_chat.amr'
                    });
                },
                // 停止并且发送录音消息
                fnStopsendAudio: function () {
                    api.stopRecord(function (ret, err) {
                        if (ret) {
                            if (ret.duration > 0) {
                              // 调取环信发送文本接口
                              var ext = {
                                  "fromId": String($api.getStorage("emp").userId),
                                  "toId": String(api.pageParam.userId),
                                  "consultationId": String(api.pageParam.consultationId),
                                  "toName": that.user.name,
                                  "toAvatar":  that.user.avatar,
                                  "fromName": that.doctor.name,
                                  "fromAvatar": that.doctor.avatar,
                                  "department": that.doctor.department,
                                  "hospital": that.doctor.hospital,
                                  "jobtitle": that.doctor.jobtitle,
                                  "card": String(isCard)
                              }
                              // 调取环信发送文本接口
                              var str = isCard == 2 ? encodeURIComponent(JSON.stringify(msg)) : msg;
                              var currentUserId = $api.getStorage("emp").userId;
                              var userId = api.pageParam.userId;
                              var conversationsId = (typeof api.pageParam.conversationsId == "undefined") ? String(userId) : api.pageParam.conversationsId;
                              fnEaseChatSendVoice(conversationsId, ret.path, currentUserId, userId, ret.duration,ext);
                            } else {
                                toast('录音时间太短了，请重新录音');
                            }
                        } else {
                            toast('糟糕，开小差了:', JSON.stringify(err));
                        }
                    });
                },

                // 发送文本消息
                fnSendText: function (msg, isCard) {
                    var that = this;
                    // 调取环信发送文本接口
                    var ext = {
                        "fromId": String($api.getStorage("emp").userId),
                        "toId": String(api.pageParam.userId),
                        "consultationId": String(api.pageParam.consultationId),
                        "toName": that.user.name,
                        "toAvatar":  that.user.avatar,
                        "fromName": that.doctor.name,
                        "fromAvatar": that.doctor.avatar,
                        "department": that.doctor.department,
                        "hospital": that.doctor.hospital,
                        "jobtitle": that.doctor.jobtitle,
                        "card": String(isCard)
                    }
                    // 调取环信发送文本接口
                    var str = isCard == 2 ? encodeURIComponent(JSON.stringify(msg)) : msg;
                    var currentUserId = $api.getStorage("emp").userId;
                    var userId = api.pageParam.userId;
                    var conversationsId = (typeof api.pageParam.conversationsId == "undefined") ? String(userId) : api.pageParam.conversationsId;
                    that.fnEaseChatSendText(conversationsId, str,currentUserId, userId, ext)
                },
                //发送图片消息
                fnSendImage: function () {
                    var that = this;
                    var trs = isJuris('photos', '需要获取您的相册权限，才能上传照片，请问是否同意？'); //相机权限
                    if (!trs) {
                        toast("获取相册权限失败,请重新授权")
                        return;
                    }
                    api.getPicture({
                        sourceType: 'album',
                        encodingType: 'jpg',
                        mediaValue: 'pic',
                        destinationType: 'url',
                        allowEdit: true,
                        quality: 70,
                        targetWidth: 780,
                        targetHeight: 1040,
                        saveToPhotoAlbum: false
                    }, function (ret, err) {
                        if (ret) {
                          // 扩展信息
                          var ext = {
                              "fromId": String($api.getStorage("emp").userId),
                              "toId": String(api.pageParam.userId),
                              "consultationId": String(api.pageParam.consultationId),
                              "toName": that.user.name,
                              "toAvatar":  that.user.avatar,
                              "fromName": that.doctor.name,
                              "fromAvatar": that.doctor.avatar,
                              "department": that.doctor.department,
                              "hospital": that.doctor.hospital,
                              "jobtitle": that.doctor.jobtitle
                          }
                          var conversationsId = (typeof api.pageParam.conversationsId == "undefined") ? String(api.pageParam.consultationId) : api.pageParam.conversationsId;
                          var currentUserId = $api.getStorage("emp").userId;
                          var userId = api.pageParam.userId;
                          that.fnEaseChatSendImage(conversationsId, ret.data, currentUserId,userId, ext);
                        } else {
                            toast(err.msg);
                        }
                    });
                } //end
            },
            filters: {
                formatTime: function (v) {
                    return new Date(v).popular();
                }
            }, //endfilters

        }) //endvue
    }
</script>

</html>
